/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline;

namespace Cobalt.Content.Pipeline.Graphics.Font
{
	[ContentImporter( ".fnt", DisplayName = "Cobalt Font Importer", DefaultProcessor = "SpriteFontProcessor" )]
	public class SpriteFontImporter : ContentImporter<SpriteFontDescription>
	{
		private int ParseNum( string s )
		{
			s = s.ToLower();

			if( s.StartsWith( "0x" ) )
			{
				return int.Parse( s.Substring( 2 ), System.Globalization.NumberStyles.AllowHexSpecifier );
			}

			return int.Parse( s );
		}

		public override SpriteFontDescription Import( string filename, ContentImporterContext context )
		{
			SpriteFontDescription ret = new SpriteFontDescription();
			ret.Identity = new ContentIdentity( filename );

			using( ContentLineReader reader = ContentLineReader.FromFile( filename ) )
			{
				ret.FontName = reader.ReadRequiredLine();
				ret.Style = (System.Drawing.FontStyle)Enum.Parse( typeof( System.Drawing.FontStyle ),
					reader.ReadRequiredLine() );

				string sizeStr = reader.ReadRequiredLine();
				string[] sizes = sizeStr.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries );
				foreach( string s in sizes )
				{
					try
					{
						ret.Sizes.Add( float.Parse( s ) );
					}
					catch( FormatException ex )
					{
						throw new InvalidContentException( "Invalid token. Expected a number.", reader.LastLineIdentity, ex );
					}
				}

				for( ; ; )
				{
					string s = reader.ReadLine();

					if( s == null )
						break;

					if( string.Compare( s, "Default", StringComparison.InvariantCultureIgnoreCase ) == 0 )
					{
						ret.Characters.AddDefaultCharacters();
						continue;
					}

					int idx = s.IndexOf( '-' );
					if( idx == -1 )
					{
						int ch = ParseNum( s );
						ret.Characters.Add( (char)ch );
					}
					else
					{
						int cs = ParseNum( s.Substring( 0, idx ) );
						int ce = ParseNum( s.Substring( idx + 1 ) );

						for( int ch = cs; ch <= ce; ch++ )
							ret.Characters.Add( (char)ch );
					}
				}
			}

			ret.Name = string.Format( "{0}_{1}", ret.FontName, ret.Style );

			return ret;
		}
	}
}